﻿// <copyright file="TagValueParser.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xin-ping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Converts the values of specified type to text and parses them from the representing text.
    /// </summary>
    public abstract class TagValueParser
    {
        /// <summary>
        /// The register value parsers.
        /// </summary>
        private static Dictionary<Type, TagValueParser> parsers;

        /// <summary>
        /// Registers a value parser.
        /// </summary>
        /// <param name="valueType">Type of the value.</param>
        /// <param name="parser">The parser to register.</param>
        public static void RegisterParser(Type valueType, TagValueParser parser)
        {
            if (parsers == null)
            {
                parsers = new Dictionary<Type, TagValueParser>();
            }

            if (parsers.ContainsKey(valueType))
            {
                parsers[valueType] = parser;
            }
            else
            {
                parsers.Add(valueType, parser);
            }
        }

        /// <summary>
        /// Unregisters a value parser.
        /// </summary>
        /// <param name="valueType">Type of the value.</param>
        public static void UnregisterParser(Type valueType)
        {
            if (parsers != null && parsers.ContainsKey(valueType))
            {
                parsers.Remove(valueType);
            }
        }

        /// <summary>
        /// Clears all the registered parsers.
        /// </summary>
        public static void ClearRegister()
        {
            if (parsers != null)
            {
                parsers.Clear();
            }
        }

        /// <summary>
        /// Determines whether the parser for the specified value type is registered.
        /// </summary>
        /// <param name="valueType">Type of the value.</param>
        /// <returns>
        /// <c>true</c> if the parser is registered; otherwise, <c>false</c>.
        /// </returns>
        public static bool ContainsParser(Type valueType)
        {
            if (parsers == null)
            {
                return false;
            }

            return parsers.ContainsKey(valueType);
        }

        /// <summary>
        /// Parses the value from a string.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="text">The text to parse.</param>
        /// <returns>The value parsered from the text.</returns>
        public static string ConvertToString<T>(T value)
        {
            Type valType = typeof(T);
            if (parsers.ContainsKey(valType))
            {
                return parsers[valType].ToString<T>(value);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Parses the value from a string.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="text">The text to parse.</param>
        /// <returns>The value parsered from the text.</returns>
        public static T Parse<T>(string text)
        {
            Type valType = typeof(T);
            if (parsers.ContainsKey(valType))
            {
                return parsers[valType].ParseString<T>(text);
            }
            else
            {
                return default(T);
            }
        }

        /// <summary>
        /// Converts the value of specified type to a string.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="value">The value to convert.</param>
        /// <returns>The string representing the value.</returns>
        public abstract string ToString<T>(T value);

        /// <summary>
        /// Parses the value from a string.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="text">The text to parse.</param>
        /// <returns>The value parsered from the text.</returns>
        public abstract T ParseString<T>(string text);
    }
}
